package ca.nfb.media.nfbPlayer.views
{
	import ca.nfb.media.nfbPlayer.ComModule;
	import ca.nfb.media.nfbPlayer.NFBPyroBrew;
	import ca.turbulent.media.Pyro;
	import ca.turbulent.media.events.ErrorEvent;
	import ca.turbulent.media.events.PyroEvent;
	import ca.turbulent.media.events.StatusUpdateEvent;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.FullScreenEvent;
	import flash.events.MouseEvent;
	
	public class NFBPlayerView extends MovieClip
	{
		public var player				:NFBPyroBrew;
		public var core					:NFBPlayer;
		public var previousState		:String;
		
		
		public function NFBPlayerView()
		{
			super();
			initializeView();
		}
		
		private function initializeView():void
		{
			core = ComModule.getInstance().core;
			player = ComModule.getInstance().core.player;
			core.registerView({instanceName:this.name, instance:this});
			this.addEventListener(Event.ADDED_TO_STAGE, addedToStage, false, 0, true);
			registerEvents();
			checkLang();
		}
		
		protected function checkLang():void { } 
		
		public function lockUI(lock:Boolean, assets:Array):void
		{
			for(var i:int=0; i<assets.length; ++i)
			{
				var localAsset:MovieClip = assets[i];
				
				localAsset.buttonMode 		= (lock==true) ? false : true;
				localAsset.mouseEnabled 	= (lock==true) ? false : true;
				localAsset.enabled 			= (lock==true) ? false : true;
				localAsset.mouseChildren	= (lock==true) ? false : true;
				
				for(var j:int=0; j<localAsset.numChildren; ++j)
				{
					var subclip:* = localAsset.getChildAt(j);
					
					if(subclip is MovieClip)
						lockUI(lock,[subclip]);
				}
			} 
		}
		
		public function setProperties(assets:Array, props:Object, buttonHandlingMethod:Function=null):void
		{
			for(var i:int=0; i<assets.length; i++)
			{
				var localAsset = assets[i];
				
				if(localAsset is MovieClip)
				{		
					for(var e:String in props)
						if(localAsset.hasOwnProperty(e)) 
							localAsset[e] = props[e];
					
					if (buttonHandlingMethod != null)
					{
						localAsset.addEventListener(MouseEvent.MOUSE_DOWN, buttonHandlingMethod, false, 0, true);
						localAsset.addEventListener(MouseEvent.MOUSE_OVER, buttonHandlingMethod, false, 0, true);
						localAsset.addEventListener(FocusEvent.FOCUS_IN, focusedIn, false, 0, true);
						localAsset.addEventListener(FocusEvent.FOCUS_OUT, focusedOut, false, 0, true);
						localAsset.addEventListener(MouseEvent.MOUSE_OUT, buttonHandlingMethod, false, 0, true);
						localAsset.addEventListener(MouseEvent.CLICK, buttonHandlingMethod, false, 0, true);
					}
				}
			}	
		}
		
		public function clear():void
		{
			if (ComModule.getInstance().core.registeredViews[this.name])
				ComModule.getInstance().core.registeredViews[this.name] = null;
		}
		
		public function close(evt:*=null):void
		{
			this.visible = false;
		}
		
		protected function focusedIn(evt:FocusEvent):void
		{
			
		}
		
		protected function focusedOut(evt:FocusEvent):void
		{
			
		}
		
		public function removeTabIndices(items:Array):void
		{
			for (var i:Number=0; i<items.length; ++i)
			{
				items[i].tabIndex = -1;
			}
		}
		
		
		private function clearEvents():void
		{
			if (player.hasEventListener(ErrorEvent.CONNECTION_ERROR))
				player.removeEventListener(ErrorEvent.CONNECTION_ERROR, connError);
				
			if (player.hasEventListener(StatusUpdateEvent.STATUS_UPDATE))
				player.removeEventListener(StatusUpdateEvent.STATUS_UPDATE, streamStatusUpdated);
				
			if (player.hasEventListener(PyroEvent.COMPLETED))
				player.removeEventListener(PyroEvent.COMPLETED, streamCompleted);
			
			if (player.hasEventListener(PyroEvent.STARTED))	
				player.removeEventListener(PyroEvent.STARTED, streamStarted);
		
			if (player.hasEventListener(PyroEvent.STOPPED))
				player.removeEventListener(PyroEvent.STOPPED, streamStopped);
				
			if (core.hasEventListener(NFBPlayer.STATE_UPDATE_EVENT))	
				core.removeEventListener(NFBPlayer.STATE_UPDATE_EVENT, coreStateUpdated);
		}
		
		protected function registerEvents():void
		{
			clearEvents();
			player.addEventListener(PyroEvent.NEW_STREAM_INIT, newStreamInit, false, 0, true);
			player.addEventListener(ErrorEvent.CONNECTION_ERROR, connError, false, 0, true);
			player.addEventListener(StatusUpdateEvent.STATUS_UPDATE, streamStatusUpdated, false, 0, true);
			player.addEventListener(PyroEvent.COMPLETED, streamCompleted, false, 0, true);
			player.addEventListener(PyroEvent.STARTED, streamStarted, false, 0, true);
			player.addEventListener(PyroEvent.STOPPED, streamStopped, false, 0, true);
			player.addEventListener(PyroEvent.SIZE_UPDATE, sizeChanged, false, 0, true);
			
			
			core.addEventListener(NFBPlayer.STATE_UPDATE_EVENT, coreStateUpdated, false, 0, true);
			
			this.addEventListener(Event.REMOVED_FROM_STAGE, removedFromStage, false, 0, true);
		}
		
		protected function registerStageEvents():void
		{
			stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullscreenHandler, false, 0, true);
			if (core.resizable)
				stage.addEventListener(Event.RESIZE, stageResized);
				
		}
		
		protected function addedToStage(evt:Event):void 
		{ 
			registerStageEvents();	
		}
		
		
		
		protected function connError(evt:ErrorEvent=null):void { }
		protected function coreStateUpdated(evt:Event=null):void { }
		protected function fullscreenHandler(evt:FullScreenEvent):void {}
		protected function newStreamInit(evt:PyroEvent):void {}
		protected function removedFromStage(evt:Event=null):void { }
		
		protected function sizeChanged(evt:PyroEvent=null):void{ }
		protected function streamStatusUpdated(evt:StatusUpdateEvent):void { }
		protected function streamCompleted(evt:PyroEvent):void { }
		protected function streamStarted(evt:PyroEvent):void { }
		protected function streamStopped(evt:PyroEvent):void { }
		
		protected function stageResized(evt:Event=null):void {}
	}
}